//
//  AfanHomeController.swift
//  weibo
//
//  Created by afan on 16/3/25.
//  Copyright © 2016年 阿范. All rights reserved.
//

import UIKit

class AfanHomeController: AfanBaseController {

    
    
    private let ReuseIdentifier = "ReuseIdentifier"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !AfanUserAccountViewModel.sharedInstance.userLogin{
            return
        }
        setupNavigationBar()
        setupTitleButton()
        prepareTableView()
        
        AfanStatusViewModel.sharedInstance.loadStatus { (statuses, error) -> () in
            if error != nil {
                print("加载失败")
                return
            }
            self.statuses = statuses
        }
        
        
        }
    
    var statuses: [AfanStatus]?{
        didSet {
            self.tableView.reloadData()
        }
    }
    
    private func prepareTableView() {
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: ReuseIdentifier)
    }
    
    
    ///设置导航栏 titleView
    private func setupTitleButton() {
        
        AfanUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil{
                print("获取用户信息失败")
                return
            }
            
            let name = AfanUserAccountViewModel.sharedInstance.userAccount?.screen_name
            let titleButton = AfanHomeTitleButton(title: name!, imageName: "navigationbar_arrow_down")
            titleButton.addTarget(self, action: "didClickTitleButton:", forControlEvents: UIControlEvents.TouchUpInside)
            self.navigationItem.titleView = titleButton
        }
    }
    
    //MARK: 点击事件
    @objc private func didClickTitleButton(button:AfanHomeTitleButton) {
        button.selected = !button.selected
        
        UIView.animateWithDuration(1) { () -> Void in
            button.imageView?.transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
        }
        if button.selected {
            
           
            let popoverVC = AfanPopoverViewController()
            
            // 设置modal的显示样式
            popoverVC.modalPresentationStyle = .Popover
            
            // 设置popoverPresentationController的代理,实现代理的方法,来让popover不要适配iPhone
            popoverVC.popoverPresentationController?.delegate = self
            
            // popover的箭头指向titeView,默认指向左上角
            popoverVC.popoverPresentationController?.sourceView = navigationItem.titleView
            // 设置sourceView.bounds给sourceRect
            popoverVC.popoverPresentationController?.sourceRect = navigationItem.titleView!.bounds
            // 设置popover显示的大小
            popoverVC.preferredContentSize = CGSize(width: 200, height: 44 * popoverVC.titles.count)
            
            // 弹出
            presentViewController(popoverVC, animated: true, completion: nil)
        }

    }
    
    
    
    
    ///导航栏按钮
    private func setupNavigationBar() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: "didClickLeftItem")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: "didClickRightItem")
    }

    func didClickLeftItem() {
        print( "didClickLeftItem")
    }
    
    func didClickRightItem() {
        print("didClickRightItem")
    }
}

extension AfanHomeController{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(ReuseIdentifier, forIndexPath: indexPath)

        cell.textLabel?.text = statuses?[indexPath.row].text
        
        return cell
    }
}







extension AfanHomeController: UIPopoverPresentationControllerDelegate {
    // 不让popover适配iPhone,默认适配.全屏的,不适配就是popover的效果
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None
    }
    
    // 系统会询问我们是否需要关闭popover
    func popoverPresentationControllerShouldDismissPopover(popoverPresentationController: UIPopoverPresentationController) -> Bool {
        // 让箭头转下来
        let titleButton = navigationItem.titleView as! AfanHomeTitleButton
        didClickTitleButton(titleButton)
        
        return true
}
}